import { Component, OnInit, Input, Injector, Type, TemplateRef, ViewChild } from '@angular/core';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { HttpService } from "../../core/service/http.service";
import { Router, Route, ActivatedRoute } from "@angular/router";
import {
    AndOr, Compare, ExpressionPair, PagedResultDto, PagerSearchInput, ElementViewType, BaseComponentConfig, SonPagerSearchInput,
    SystemFormControl, FieldType, CustomNavigation, ElementShowType, KeyValueOutput, SystemFormControlOutput,
    DrillRoleOutput,
    DrillFlowOutput,
    NavigatType
} from "../..";
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import {
    FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators,
    FormControl
} from "@angular/forms";
import { map } from 'rxjs/operators';
import { DrillGroupComponent } from './drill-group.component';
import { DrillFormComponent } from './drill-form.component';

@Component({
    selector: 'app-drill-flow',
    templateUrl: '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.html',
    styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class DrillFlowComponent extends NgZorroPageBaseComponent implements OnInit {
    constructor(injector: Injector) {
        super(injector,
            new BaseComponentConfig(
                "DrillSubject/GetPagerDrillFlows",
                "DrillSubject/CreateDrillFlow",
                "DrillSubject/UpdateDrillFlow",
                "DrillSubject/GetDrillFlow",
                "DrillSubject/DeteleDrillFlow",
                "id",
                "DrillFlow",
                "演练流程",
                new Array<CustomNavigation>(
                    new CustomNavigation('流程分组', '', ElementShowType.TableOperation, "anticon anticon-layout", NavigatType.Model, 'group'),
                    new CustomNavigation(
                        '流程表单', '', ElementShowType.TableOperation, "anticon anticon-schedule", NavigatType.Model, 'form'),
                ),
                "drillSubjectId"
            ));
        this.showTitile = true;
        this.isNavigation = true;
    }
    ngOnInit() {
        this.isSpinning = true;
        this.route.queryParams
            .pipe(map(params => params['caption']))
            .subscribe(e => this.componentConfig.ModuleCaption = `${this.componentConfig.ModuleCaption}-[${e}]`);
        this.route.queryParamMap
            .pipe(map(params => params.get('id') || null))
            .subscribe(e => {
                this._pKeyFieldValue = Number(e);
                if (!this._pKeyFieldValue) {
                    this.message.error('无法确定主键信息!');
                } else {
                    this.isSpinning = false;
                }
                this.IniObserver.subscribe(sb => {
                    this.getEntityList();
                });
            });
    }

    getNewEntity(): DrillFlowOutput {
        return new DrillFlowOutput();
    }

    navigateCustom(url: string, pid: any) {
        throw new Error("Method not implemented.");
    }

    showModalForComponent(entity: DrillFlowOutput, tag: string) {
        const keyValue = entity.id;
        const component = <Type<any>>(tag === 'group' ? DrillGroupComponent : DrillFormComponent);
        const subscription = this.nzModalService.create({
            nzTitle: `${entity.flowName}`,
            nzContent: component,
            nzComponentParams: {
                pKeyFieldValue: keyValue,
                caption: entity.flowName
            },
            nzFooter: null,
            nzWidth: document.documentElement.clientWidth - 60,
        });
    }
}
